﻿using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using LogInModule.Models;
using YourWorld.Common;
using LogInModule.Views;
using LogInModule.ViewModels;
using YourWorld.Common.Infrastucture.Events;
using BusinessObjects;

namespace LogInModule
{
    public class LoginInitModule : IModule
    {
        private IUnityContainer container;
        private IRegionManager regionManager;

        public LoginInitModule( IRegionManager regionManager, IUnityContainer container )
        {
            this.regionManager = regionManager;
            this.container = container;
        }

        public void Initialize()
        {
            this.RegisterTypes();
        }


        public void PlayerLogout( object o )
        {
            if ( this.regionManager.Regions[RegionNames.HostRegion].Views.Contains( this.container.Resolve<LoginMainView>() ) )
                this.regionManager.Regions[RegionNames.HostRegion].Activate( this.container.Resolve<LoginMainView>() );

            if ( this.regionManager.Regions[RegionNames.LoginContent].Views.Contains( this.container.Resolve<GameInit>() ) )
                this.regionManager.Regions[RegionNames.LoginContent].Activate( this.container.Resolve<GameInit>() );
        }

        private void RegisterTypes()
        {
            this.container.RegisterType<ILoginModel, LoginModel>( new ContainerControlledLifetimeManager() );

            this.container.RegisterType<LoginMainView>( new ContainerControlledLifetimeManager() );
            this.container.RegisterType<LoginMainViewModel>( new ContainerControlledLifetimeManager() );

            this.container.RegisterType<UserLogInViewModel>();
            this.container.RegisterType<UserLogInView>();

            this.container.RegisterType<GameInit>( new ContainerControlledLifetimeManager() );
            this.container.RegisterType<GameInitViewModel>( new ContainerControlledLifetimeManager() );
            this.container.RegisterType<GameModeSelection>();
            this.container.RegisterType<GameModeSelectionViewModel>( new ContainerControlledLifetimeManager() );
            this.container.RegisterType<MapSelection>();
            this.container.RegisterType<MapSelectionViewModel>( new ContainerControlledLifetimeManager() );

            this.regionManager.RegisterViewWithRegion( RegionNames.HostRegion, typeof( LoginMainView ) );
            this.regionManager.RegisterViewWithRegion( RegionNames.LoginContent, typeof( GameInit ) );
        }
    }
}
